package com.chw.admin.usergroup;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.chw.annotation.PermissionSecurity;
import com.chw.common.ServiceData;
import com.chw.utils.LogUtil;

@Controller
@RequestMapping("/sysGroup")
public class SysUserGroupController {
	
	private static LogUtil log = LogUtil.newInstance(SysUserGroupController.class);
	
	@Autowired
	private ISysUserGroupService sysUserGroupService;
	
	@RequestMapping(value = "/pageSysUserGroup", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="用户组管理",btnfunc="select_group")
	public ServiceData page(HttpServletRequest request, SysGroup userGroup, Integer currentPage, Integer pageSize)
			throws Exception {
		log.info("page");
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		PageInfo<SysGroup> users = sysUserGroupService.page(userGroup, currentPage, pageSize);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(users);
		return ret;
	}
	
	@RequestMapping(value = "/get", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="用户组管理",btnfunc="select_group")
	public ServiceData get(HttpServletRequest request, String id)
			throws Exception {
		log.info("page");
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		SysGroup group = sysUserGroupService.selectById(id);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(group);
		return ret;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="新增",module="用户组管理",btnfunc="add_group")
	public ServiceData save(HttpServletRequest request,SysGroup userGroup) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		sysUserGroupService.saveOrUpdate(userGroup);
	    ret.setCode(request, ServiceData.RetCode.Success);
		return ret;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="修改",module="用户组管理",btnfunc="update_group")
	public ServiceData update(HttpServletRequest request,SysGroup userGroup) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		sysUserGroupService.saveOrUpdate(userGroup);
	    ret.setCode(request, ServiceData.RetCode.Success);
		return ret;
	}
	
	@RequestMapping(value = "/updateStatus", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="启用禁用",module="用户组管理",btnfunc="update_group_status")
	public void updateStatus(HttpServletRequest request, String id, String status) throws Exception {
		sysUserGroupService.updateStatus(id, status);
	}
	
	@RequestMapping(value="/delete", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="删除",module="用户组管理",btnfunc="delete_group")
	public void delete(HttpServletRequest request, String id) throws Exception{
		log.info("delete");
		sysUserGroupService.delete(id);
    }
	
	@RequestMapping(value="/addSysUserToGroup", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="新增",module="用户组的用户管理",btnfunc="")
	public void addSysUserToGroup(HttpServletRequest request,SysGroupUser groupUser) throws Exception{
		log.info("addSysUserToGroup");
		sysUserGroupService.addSysUserToGroup(groupUser);
    }
	
	@RequestMapping(value="/getUserByGroup", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="用户组的用户管理",btnfunc="")
	public ServiceData getUserByGroup(HttpServletRequest request,SysGroupUser groupUser) throws Exception{
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		List<Map<String,Object>> list = sysUserGroupService.getUserByGroup(groupUser);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(list);
		return ret;
    }
	
	@RequestMapping(value="/deleteUserFormGroup", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="删除",module="用户组的用户管理",btnfunc="")
	public void deleteUserFormGroup(HttpServletRequest request,SysGroupUser groupUser) throws Exception{
		log.info("deleteUserFormGroup");
		sysUserGroupService.deleteUserFormGroup(groupUser);
    }
	
	@RequestMapping(value = "/hasNameSysUserGroup", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="用户组管理",btnfunc="select_group")
	public ServiceData hasGroupName(HttpServletRequest request, String name,String id)
			throws Exception {
		ServiceData ret = new ServiceData();
		boolean flag = sysUserGroupService.hasGroupName(name,id);
	    ret.setCode(request, ServiceData.RetCode.Success);
	    ret.setBo(flag);
		return ret;
	}
	
}
